{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x_data.shape:  (47, 2)\n",
      "y_data.shape:  (47,)\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from matplotlib import pyplot as plt\n",
    "\n",
    "# load and process data\n",
    "data = np.loadtxt('../data_sets/ex1data2.txt', delimiter = ',')\n",
    "x_data = data[:, 0:2]\n",
    "y_data = data[:, 2]\n",
    "print('x_data.shape: ', x_data.shape)\n",
    "print('y_data.shape: ', y_data.shape)\n",
    "\n",
    "# initialization\n",
    "m = y_data.size\n",
    "theta = np.zeros(3)\n",
    "num_iteration = 100\n",
    "alpha = 1.2\n",
    "J = np.zeros(num_iteration)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Feature scaling - Normalization and standardization \n",
    "# z-score standardization\n",
    "def feature_scaling(x):\n",
    "    mu = np.mean(x, axis = 0)\n",
    "    sigma = np.std(x, axis = 0)\n",
    "    x = (x - mu) / sigma\n",
    "    return x\n",
    "\n",
    "def cost_function(theta, x, y, m):\n",
    "    # hypothesis\n",
    "    h_x = x @ theta  # x->[47,3],theta->[3,1],h_x->[47,1]\n",
    "    return np.sum((h_x-y)**2)/(2*m), h_x\n",
    "\n",
    "def gradient_descent(theta, x, y, m, num_iteration, J):\n",
    "    for i in range(num_iteration):\n",
    "        J[i], h_x = cost_function(theta, x, y, m)\n",
    "        theta = theta - (alpha/m) * (x.T @ (h_x - y))\n",
    "        print('num_iteration:{}, cost_values:{}'.format(1+i, J[i]))\n",
    "    return theta, J"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Start training ... \n",
      "num_iteration:1, cost_values:65591548106.45744\n",
      "num_iteration:2, cost_values:7828546391.702185\n",
      "num_iteration:3, cost_values:4595162832.427102\n",
      "num_iteration:4, cost_values:3877239479.271789\n",
      "num_iteration:5, cost_values:3426198089.9162583\n",
      "num_iteration:6, cost_values:3092667220.980161\n",
      "num_iteration:7, cost_values:2840704423.755805\n",
      "num_iteration:8, cost_values:2649476645.502242\n",
      "num_iteration:9, cost_values:2504158970.4995604\n",
      "num_iteration:10, cost_values:2393688550.733585\n",
      "num_iteration:11, cost_values:2309699943.2248154\n",
      "num_iteration:12, cost_values:2245842949.6013985\n",
      "num_iteration:13, cost_values:2197291682.13135\n",
      "num_iteration:14, cost_values:2160377448.840796\n",
      "num_iteration:15, cost_values:2132310999.2483287\n",
      "num_iteration:16, cost_values:2110971648.3883145\n",
      "num_iteration:17, cost_values:2094747012.4702222\n",
      "num_iteration:18, cost_values:2082411172.5632002\n",
      "num_iteration:19, cost_values:2073032043.9423954\n",
      "num_iteration:20, cost_values:2065900948.329989\n",
      "num_iteration:21, cost_values:2060479066.754979\n",
      "num_iteration:22, cost_values:2056356726.8768492\n",
      "num_iteration:23, cost_values:2053222448.5204477\n",
      "num_iteration:24, cost_values:2050839408.5223005\n",
      "num_iteration:25, cost_values:2049027546.5920439\n",
      "num_iteration:26, cost_values:2047649960.1239738\n",
      "num_iteration:27, cost_values:2046602559.9632235\n",
      "num_iteration:28, cost_values:2045806205.5258293\n",
      "num_iteration:29, cost_values:2045200725.0096705\n",
      "num_iteration:30, cost_values:2044740368.868945\n",
      "num_iteration:31, cost_values:2044390353.0209558\n",
      "num_iteration:32, cost_values:2044124230.593112\n",
      "num_iteration:33, cost_values:2043921893.6217742\n",
      "num_iteration:34, cost_values:2043768053.7131982\n",
      "num_iteration:35, cost_values:2043651086.8666823\n",
      "num_iteration:36, cost_values:2043562155.1758716\n",
      "num_iteration:37, cost_values:2043494539.044754\n",
      "num_iteration:38, cost_values:2043443129.461223\n",
      "num_iteration:39, cost_values:2043404041.9648635\n",
      "num_iteration:40, cost_values:2043374323.140733\n",
      "num_iteration:41, cost_values:2043351727.4620621\n",
      "num_iteration:42, cost_values:2043334547.6203473\n",
      "num_iteration:43, cost_values:2043321485.5226924\n",
      "num_iteration:44, cost_values:2043311554.209122\n",
      "num_iteration:45, cost_values:2043304003.2788033\n",
      "num_iteration:46, cost_values:2043298262.1904476\n",
      "num_iteration:47, cost_values:2043293897.1527488\n",
      "num_iteration:48, cost_values:2043290578.3475099\n",
      "num_iteration:49, cost_values:2043288055.008888\n",
      "num_iteration:50, cost_values:2043286136.4757566\n",
      "num_iteration:51, cost_values:2043284677.7855358\n",
      "num_iteration:52, cost_values:2043283568.720946\n",
      "num_iteration:53, cost_values:2043282725.4820952\n",
      "num_iteration:54, cost_values:2043282084.3546388\n",
      "num_iteration:55, cost_values:2043281596.8955824\n",
      "num_iteration:56, cost_values:2043281226.27297\n",
      "num_iteration:57, cost_values:2043280944.4829037\n",
      "num_iteration:58, cost_values:2043280730.2335858\n",
      "num_iteration:59, cost_values:2043280567.3365371\n",
      "num_iteration:60, cost_values:2043280443.4834073\n",
      "num_iteration:61, cost_values:2043280349.31597\n",
      "num_iteration:62, cost_values:2043280277.7190204\n",
      "num_iteration:63, cost_values:2043280223.2827597\n",
      "num_iteration:64, cost_values:2043280181.894034\n",
      "num_iteration:65, cost_values:2043280150.425549\n",
      "num_iteration:66, cost_values:2043280126.499577\n",
      "num_iteration:67, cost_values:2043280108.3082922\n",
      "num_iteration:68, cost_values:2043280094.4771802\n",
      "num_iteration:69, cost_values:2043280083.9611723\n",
      "num_iteration:70, cost_values:2043280075.9656901\n",
      "num_iteration:71, cost_values:2043280069.8866012\n",
      "num_iteration:72, cost_values:2043280065.2645774\n",
      "num_iteration:73, cost_values:2043280061.7503817\n",
      "num_iteration:74, cost_values:2043280059.0784843\n",
      "num_iteration:75, cost_values:2043280057.0469997\n",
      "num_iteration:76, cost_values:2043280055.502431\n",
      "num_iteration:77, cost_values:2043280054.3280714\n",
      "num_iteration:78, cost_values:2043280053.435188\n",
      "num_iteration:79, cost_values:2043280052.7563143\n",
      "num_iteration:80, cost_values:2043280052.2401574\n",
      "num_iteration:81, cost_values:2043280051.847715\n",
      "num_iteration:82, cost_values:2043280051.5493345\n",
      "num_iteration:83, cost_values:2043280051.3224711\n",
      "num_iteration:84, cost_values:2043280051.1499846\n",
      "num_iteration:85, cost_values:2043280051.0188398\n",
      "num_iteration:86, cost_values:2043280050.9191282\n",
      "num_iteration:87, cost_values:2043280050.8433166\n",
      "num_iteration:88, cost_values:2043280050.7856743\n",
      "num_iteration:89, cost_values:2043280050.741849\n",
      "num_iteration:90, cost_values:2043280050.7085278\n",
      "num_iteration:91, cost_values:2043280050.6831932\n",
      "num_iteration:92, cost_values:2043280050.6639316\n",
      "num_iteration:93, cost_values:2043280050.649286\n",
      "num_iteration:94, cost_values:2043280050.6381497\n",
      "num_iteration:95, cost_values:2043280050.6296842\n",
      "num_iteration:96, cost_values:2043280050.6232479\n",
      "num_iteration:97, cost_values:2043280050.6183536\n",
      "num_iteration:98, cost_values:2043280050.6146321\n",
      "num_iteration:99, cost_values:2043280050.611803\n",
      "num_iteration:100, cost_values:2043280050.6096523\n",
      "theta: [340412.65957447 109447.73880033  -6578.41252348]\n",
      "Complete the training !!! \n",
      "Normal equation's theta:  [340412.65957447 109447.79646964  -6578.35485416]\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAAERCAYAAAB4jRxOAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAHYdJREFUeJzt3XuUHXWZ7vHvU7V30iGBBJOWwSDEgCAcRi62IKiIiIgMR7zgbfCMF9bkqBxF18w4cmY5ntE1F8ejB8ejOBkU1EFGRBkd5ogCBlAHgQ5CuISIIkiGSxoQCCBJX97zR1WHnc6+VO/u6u7Ufj5r9eq9a9eueiuV9e5fv/tXbykiMDOz6ktmOwAzM5sZT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F79Nie42uIfugIC/l7E123E3lpaIkIr4NfIyszbL1KCd8mymfARpn6/wTWZK9Hpg48i1qA1li/j7w3ryUcS5ZOePG/IvOf6TDX7KRtZs9i6wV783AjRExmTa8a4CDxr+0BT5J9gG2Lo/hky3edwEwIGmQLInfkcfzMNl3D7dO/LIY+CKQSroF+Cbwrrz01cpy4Kq8vHR+fpzWo9wt08ysR3iEb2bWI5zwzcx6hBO+mVmPcMI3M+sRTvhmZj3CCd/MrEc44ZuZ9Yj/D0uzdxF5nw99AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# build the model\n",
    "print('Start training ... ')\n",
    "# add one dimension onto x_data with x0 = 1\n",
    "x0 = np.ones(m)\n",
    "x_data = feature_scaling(x_data)\n",
    "x_data = np.column_stack((x0, x_data))\n",
    "theta, J = gradient_descent(theta, x_data, y_data, m, num_iteration, J)\n",
    "# plot the cost function\n",
    "plt.xlabel('Number of iterations')\n",
    "plt.ylabel('Cost function values')\n",
    "plt.plot(J)\n",
    "print('theta:', theta)\n",
    "print('Complete the training !!! ')\n",
    "\n",
    "# Normal equation \n",
    "theta = np.linalg.inv(x_data.T @ x_data) @ x_data.T @ y_data\n",
    "print('Normal equation\\'s theta: ', theta)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
